package com.xnj.testweb.common;

import com.xnj.testweb.common.constants.Constants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel("响应状态")
@Data
public class Result<T> {

    /**
     * 状态码 200成功 500失败 401未登录
     */
    @ApiModelProperty("状态码 200成功 500失败 401未登录")
    private Integer code;

    /**
     * 后台返回给前端的数据
     */
    @ApiModelProperty("返回的数据")
    private T data;

    /**
     * 后台返回前端的提示信息
     */
    @ApiModelProperty("提示信息")
    private String message;

    /**
     * 业务是否成功执行完毕
     */
    @ApiModelProperty("业务是否成功执行完毕,true成功,false失败")
    private Boolean success;


    public static Result success() {
        Result commonResult = new Result();
        commonResult.setCode(Constants.SUCCESS);
        commonResult.setSuccess(true);
        return commonResult;
    }

    public static Result success(Object data) {
        Result commonResult = new Result();
        commonResult.setCode(Constants.SUCCESS);
        commonResult.setData(data);
        commonResult.setSuccess(true);
        return commonResult;
    }

    public static Result success(Object data, String message) {
        Result commonResult = new Result();
        commonResult.setCode(Constants.SUCCESS);
        commonResult.setData(data);
        commonResult.setMessage(message);
        commonResult.setSuccess(true);
        return commonResult;
    }

    public static Result error(String message) {
        Result commonResult = new Result();
        commonResult.setCode(Constants.ERROR);
        commonResult.setMessage(message);
        commonResult.setSuccess(false);
        return commonResult;
    }

    public static Result error(String message, Integer code) {
        Result commonResult = new Result();
        commonResult.setCode(code);
        commonResult.setMessage(message);
        commonResult.setSuccess(false);
        return commonResult;
    }
}